<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>元素删除</title>
    <script>
      let nums = [1, 2, 3, 4, 5, 5, 1, 2, 5]

      /* 根据下标删除元素 */
      /* 删除数组尾部元素 -- 修改长度即可 */
      //nums.length -= 2

      /* 删除数组头部元素 */
      /* for (let i = 1; i < nums.length; i++) {
        nums[i - 1] = nums[i]
      }
      nums.length -= 1 */

      /* 删除数组中间的元素 */
      /* let pos = 2
      for (let i = pos + 1; i < nums.length; i++) {
        nums[i - 1] = nums[i]
      }
      nums.length -= 1 */

      /* 根据元素删除 */
      //例如删除数组中的所有5
      let temp = []
      let tempIdx = 0
      for (let i = 0; i < nums.length; i++) {
        if (nums[i] != 5) {
          temp[tempIdx++] = nums[i]
        }
      }
      nums = temp
      console.log(nums)
    </script>
  </head>
  <body></body>
</html>
